﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing;

namespace UsrCustomFun.ClassDGVPrint
{
    class ClassDgvPrintFunction
    {
        #region [参数]
        /// <summary>
        /// 实例化打印类GridPrinter
        /// </summary>
        ClassGridPrinter gridPrinter;
        /// <summary>
        /// PrintDocument实例
        /// </summary>
        private PrintDocument Pd_print = new PrintDocument();
        /// <summary>
        /// DataGridView实例
        /// </summary>
        private DataGridView Dgv_print;
        /// <summary>
        /// string_Title实例
        /// </summary>
        private string Title_print;
        /// <summary>
        /// string_Font_Family字体
        /// </summary>
        private string Font_Family_print;
        /// <summary>
        /// int字体大小
        /// </summary>
        private int Font_size_print;

        /// <summary>
        /// 打印方向，True为横向，False为纵向
        /// </summary>
        private bool LandScape_print;

        /// <summary>
        /// 上下页边距
        /// </summary>
        private int Margin_top_bottom_print;

        /// <summary>
        /// 左右页边距
        /// </summary>
        private int Margin_left_right_print;
        /// <summary>
        /// 是否显示标题
        /// </summary>
        private bool IsShowTitle = false;
        /// <summary>
        /// 标题颜色
        /// </summary>
        private Color PrTitleColor = Color.Blue;
        /// <summary>
        /// 是否每页打印标题
        /// </summary>
        private bool TitlesInEveryPages = false;
        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="objDataGridView">DataGridView数据</param>
        /// <param name="objTitle">标题</param>
        /// <param name="objFont">字体</param>
        /// <param name="objFontSize">字体大小</param>
        /// <param name="objLandscape">打印方向</param>
        /// <param name="objMargin_y">上下页边距</param>
        /// <param name="objMargin_x">左右页边距</param>
        /// <param name="objShowTitle">是否显示标题</param>
        /// <param name="HasTitlesInEvePages">是否每页打印标题</param>
        public ClassDgvPrintFunction(DataGridView objDataGridView, string objTitle, string objFont, int objFontSize, bool objLandscape, int objMargin_y, int objMargin_x, bool objShowTitle, Color objTitleColor, bool HasTitlesInEvePages)
        {
            Dgv_print = objDataGridView;//获取DataGridView
            Title_print = objTitle;//获取打印标题
            Font_Family_print = objFont;//获取打印字体
            Font_size_print = objFontSize;//获取打印字体大小
            LandScape_print = objLandscape;//获取打印方向
            Margin_top_bottom_print = objMargin_y;//上下页边距
            Margin_left_right_print = objMargin_x;//左右页边距
            IsShowTitle = objShowTitle;//是否显示标题
            PrTitleColor = objTitleColor;//标题颜色
            TitlesInEveryPages = HasTitlesInEvePages;//是否每页打印标题
            Pd_print.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);//加载PrintDocument的PrintPage事件
            if (InitializePrinting())
            {
                PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
                printPreviewDialog.Document = Pd_print;
                ((Form)printPreviewDialog).WindowState = FormWindowState.Maximized;
                try
                {
                    printPreviewDialog.ShowDialog();
                }
                catch (Exception ec)
                {
                    MessageBox.Show(ec.Message, "打印错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// 打印初始化
        /// </summary>
        /// <returns></returns>
        private bool InitializePrinting()
        {
            PrintDialog printDialog = new PrintDialog();
            if (printDialog.ShowDialog() != DialogResult.OK)//选择打印机
            {
                return false;
            }
            Pd_print.DocumentName = Title_print;//打印页面的标题
            Pd_print.PrinterSettings = printDialog.PrinterSettings;//设置打印机
            printDialog.PrinterSettings.DefaultPageSettings.Landscape = LandScape_print;//设置打印方向
            Pd_print.DefaultPageSettings = printDialog.PrinterSettings.DefaultPageSettings;//页面设置
            Pd_print.DefaultPageSettings.Margins = new Margins(Margin_left_right_print, Margin_left_right_print, Margin_top_bottom_print, Margin_top_bottom_print);//页边距
            bool TitleMiddleShow = true;//标题居中

            if (MessageBox.Show("是否居中打印？", "打印方式", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                TitleMiddleShow = true;
            }
            else
            {
                TitleMiddleShow = false;
            }
            gridPrinter = new ClassGridPrinter(Dgv_print, Pd_print, TitleMiddleShow, IsShowTitle, Pd_print.DocumentName, new Font(Font_Family_print, Font_size_print, FontStyle.Bold, GraphicsUnit.Point), PrTitleColor, true, TitlesInEveryPages);
            return true;
        }


        /// <summary>
        /// PrintPage事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            bool more = gridPrinter.DrawDataGridView(e.Graphics);
            if (more == true)
            {
                e.HasMorePages = true;
            }
        }
    }
}
